/**
 * Created by Administrator on 2016/8/1.
 */

var uuid = require('node-uuid');
var async = require('async');
var cartDao = require('../dao/cartDao');

//确认订单页面
exports.comfirmorder = function (userid,callback) {
    cartDao.comfirmorder(userid,function (err, data) {
        callback(err, data);
    })
};

//确认订单定金待支付数据
exports.advancewait = function (reservearray,callback) {
    var payarr=[];
    var index=[];
    for(var j=0;j<reservearray.length;j++){
        var id=reservearray[j].id;
        index.push(id);
    }
    cartDao.clearcart(index,function (err,data2) {
        var out_trade_no= new Date().getTime() + "_" + Math.floor(Math.random() * 1000);//生成唯一单号
        for(var i=0;i<reservearray.length;i++){
            reservearray[i].id = uuid.unparse(uuid.v4(null, new Buffer(32), 0));
            reservearray[i].orderstatus = 4;
            reservearray[i].status = 1;
            reservearray[i].createAt = new Date();
            if(reservearray[i].deposit){
                delete(reservearray[i]["deposit"]);
            }
        }
        cartDao.advancewait(reservearray,function (err,data) {
            callback(err,{data:data,data2:data2});
        })
    })
};


//确认订单已支付定金
exports.payearnest = function (userid,orderno,source,deposit,sid,callback) {
    cartDao.payearnest(userid,orderno,source,deposit,sid,function (err,rows1) {
        var params={
            id:uuid.unparse(uuid.v4(null, new Buffer(32), 0)),
            userid:userid,
            orderno:orderno,
            source:source,
            spotid:sid,
            deposit:deposit,
            createAt:new Date(),
            isdeposit:1,
            status:1
        };
        cartDao.addpay(params,function (err,rows2) {
            callback(err,{rows1:rows1,rows2:rows2});
        })
    })
};

//购物车
exports.shoppingcart = function (userid,callback) {
    cartDao.shoppingcart(userid,function (err, data) {
        callback(err, data);
    })
};

